using System;
using System.Drawing;
using System.Drawing.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace HtmlDropDownListDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonFonts;
		private System.Windows.Forms.Button buttonColors;
		private System.Windows.Forms.TextBox textBoxHtml;
		private System.Windows.Forms.Button buttonAdd;
		private System.Windows.Forms.Label labelHtml;
		private System.Windows.Forms.Button buttonClearItems;
		private System.Windows.Forms.Button buttonCountries;
		private System.Windows.Forms.Button buttonKeys;
		private System.Windows.Forms.ComboBox comboBoxTextRendering;
		private System.Windows.Forms.Button buttonDefault;
		private FB.WinForms.Controls.HtmlDropDownList htmlDropDownList1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ImageList imageListFlags;
		private System.Windows.Forms.Label labelTextRendering;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			InitializeComponent();

			htmlDropDownList1_SelectedIndexChanged_1(this, EventArgs.Empty);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.imageListFlags = new System.Windows.Forms.ImageList(this.components);
			this.buttonFonts = new System.Windows.Forms.Button();
			this.buttonColors = new System.Windows.Forms.Button();
			this.textBoxHtml = new System.Windows.Forms.TextBox();
			this.buttonAdd = new System.Windows.Forms.Button();
			this.labelHtml = new System.Windows.Forms.Label();
			this.buttonClearItems = new System.Windows.Forms.Button();
			this.buttonCountries = new System.Windows.Forms.Button();
			this.buttonKeys = new System.Windows.Forms.Button();
			this.comboBoxTextRendering = new System.Windows.Forms.ComboBox();
			this.buttonDefault = new System.Windows.Forms.Button();
			this.htmlDropDownList1 = new FB.WinForms.Controls.HtmlDropDownList();
			this.panel1 = new System.Windows.Forms.Panel();
			this.labelTextRendering = new System.Windows.Forms.Label();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// imageListFlags
			// 
			this.imageListFlags.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
			this.imageListFlags.ImageSize = new System.Drawing.Size(32, 32);
			this.imageListFlags.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListFlags.ImageStream")));
			this.imageListFlags.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// buttonFonts
			// 
			this.buttonFonts.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonFonts.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonFonts.Location = new System.Drawing.Point(136, 228);
			this.buttonFonts.Name = "buttonFonts";
			this.buttonFonts.Size = new System.Drawing.Size(125, 24);
			this.buttonFonts.TabIndex = 1;
			this.buttonFonts.Text = "Populate with Fonts";
			this.buttonFonts.Click += new System.EventHandler(this.buttonFonts_Click);
			// 
			// buttonColors
			// 
			this.buttonColors.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonColors.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonColors.Location = new System.Drawing.Point(8, 228);
			this.buttonColors.Name = "buttonColors";
			this.buttonColors.Size = new System.Drawing.Size(125, 24);
			this.buttonColors.TabIndex = 2;
			this.buttonColors.Text = "Populate with Colors";
			this.buttonColors.Click += new System.EventHandler(this.buttonColors_Click);
			// 
			// textBoxHtml
			// 
			this.textBoxHtml.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBoxHtml.Location = new System.Drawing.Point(32, 120);
			this.textBoxHtml.Multiline = true;
			this.textBoxHtml.Name = "textBoxHtml";
			this.textBoxHtml.Size = new System.Drawing.Size(528, 48);
			this.textBoxHtml.TabIndex = 5;
			this.textBoxHtml.Text = "";
			// 
			// buttonAdd
			// 
			this.buttonAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonAdd.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonAdd.Location = new System.Drawing.Point(488, 176);
			this.buttonAdd.Name = "buttonAdd";
			this.buttonAdd.TabIndex = 6;
			this.buttonAdd.Text = "Add";
			this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
			// 
			// labelHtml
			// 
			this.labelHtml.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelHtml.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			this.labelHtml.Location = new System.Drawing.Point(32, 104);
			this.labelHtml.Name = "labelHtml";
			this.labelHtml.Size = new System.Drawing.Size(232, 16);
			this.labelHtml.TabIndex = 13;
			this.labelHtml.Text = "Enter HTML and click the Add button";
			// 
			// buttonClearItems
			// 
			this.buttonClearItems.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonClearItems.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonClearItems.Location = new System.Drawing.Point(440, 88);
			this.buttonClearItems.Name = "buttonClearItems";
			this.buttonClearItems.Size = new System.Drawing.Size(120, 24);
			this.buttonClearItems.TabIndex = 20;
			this.buttonClearItems.Text = "Clear Items";
			this.buttonClearItems.Click += new System.EventHandler(this.buttonClearItems_Click);
			// 
			// buttonCountries
			// 
			this.buttonCountries.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonCountries.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonCountries.Location = new System.Drawing.Point(8, 260);
			this.buttonCountries.Name = "buttonCountries";
			this.buttonCountries.Size = new System.Drawing.Size(125, 24);
			this.buttonCountries.TabIndex = 23;
			this.buttonCountries.Text = "Populate with Countries";
			this.buttonCountries.Click += new System.EventHandler(this.buttonCountries_Click);
			// 
			// buttonKeys
			// 
			this.buttonKeys.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonKeys.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonKeys.Location = new System.Drawing.Point(136, 260);
			this.buttonKeys.Name = "buttonKeys";
			this.buttonKeys.Size = new System.Drawing.Size(125, 24);
			this.buttonKeys.TabIndex = 24;
			this.buttonKeys.Text = "Populate with Keys";
			this.buttonKeys.Click += new System.EventHandler(this.buttonKeys_Click);
			// 
			// comboBoxTextRendering
			// 
			this.comboBoxTextRendering.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.comboBoxTextRendering.Location = new System.Drawing.Point(456, 260);
			this.comboBoxTextRendering.Name = "comboBoxTextRendering";
			this.comboBoxTextRendering.Size = new System.Drawing.Size(121, 21);
			this.comboBoxTextRendering.TabIndex = 25;
			this.comboBoxTextRendering.SelectedIndexChanged += new System.EventHandler(this.comboBoxTextRendering_SelectedIndexChanged);
			// 
			// buttonDefault
			// 
			this.buttonDefault.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonDefault.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.buttonDefault.Location = new System.Drawing.Point(264, 244);
			this.buttonDefault.Name = "buttonDefault";
			this.buttonDefault.Size = new System.Drawing.Size(125, 24);
			this.buttonDefault.TabIndex = 27;
			this.buttonDefault.Text = "Populate with Default";
			this.buttonDefault.Click += new System.EventHandler(this.buttonDefault_Click);
			// 
			// htmlDropDownList1
			// 
			this.htmlDropDownList1.ImageList = this.imageListFlags;
			this.htmlDropDownList1.Items.AddRange(new object[] {
																   "<font color=\"Black\">Where the <B>file size</B> is greater than </font><a id=\"size" +
																   "\">size</a>.",
																   "<font color=\"Black\">Where the file was <I>changed</I> before </font><a id=\"date\">" +
																   "date</a>.",
																   "<font color=\"Black\">Where the file <U>contains</U> </font><a id=\"text\">text</a>.",
																   "<brush type=\"LinearGradient\" StartingColor=\"Red\" EndingColor=\"Blue\" GradientAngle" +
																   "=\"0\">Linear Gradient Brush Example</brush>",
																   "<brush type=\"Shadow\" offset=\"2\" shadowcolor=\"Blue\">Shadow Brush Example</brush>",
																   "<img src=\"#0\"/> Australia"});
			this.htmlDropDownList1.Location = new System.Drawing.Point(24, 16);
			this.htmlDropDownList1.Name = "htmlDropDownList1";
			this.htmlDropDownList1.SelectedIndex = 0;
			this.htmlDropDownList1.Size = new System.Drawing.Size(544, 39);
			this.htmlDropDownList1.TabIndex = 0;
			this.htmlDropDownList1.SelectedIndexChanged += new System.EventHandler(this.htmlDropDownList1_SelectedIndexChanged_1);
			this.htmlDropDownList1.LinkClicked += new FB.WinForms.Controls.HtmlDropDownList.HtmlDropDownListLinkClickedEventHandler(this.htmlDropDownList1_LinkClicked);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.htmlDropDownList1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.DockPadding.All = 25;
			this.panel1.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(592, 80);
			this.panel1.TabIndex = 28;
			// 
			// labelTextRendering
			// 
			this.labelTextRendering.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelTextRendering.Location = new System.Drawing.Point(456, 240);
			this.labelTextRendering.Name = "labelTextRendering";
			this.labelTextRendering.Size = new System.Drawing.Size(100, 15);
			this.labelTextRendering.TabIndex = 29;
			this.labelTextRendering.Text = "Text Rendering:";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(13, 31);
			this.ClientSize = new System.Drawing.Size(592, 294);
			this.Controls.Add(this.labelTextRendering);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.buttonDefault);
			this.Controls.Add(this.comboBoxTextRendering);
			this.Controls.Add(this.buttonKeys);
			this.Controls.Add(this.buttonCountries);
			this.Controls.Add(this.textBoxHtml);
			this.Controls.Add(this.buttonClearItems);
			this.Controls.Add(this.labelHtml);
			this.Controls.Add(this.buttonAdd);
			this.Controls.Add(this.buttonColors);
			this.Controls.Add(this.buttonFonts);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "Form1";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();

			// The following Application.DoEvents() is required to avoid the SEHException error
			// being thrown returning from a ShowDialog call when EnableVisualStyles() has been
			// called.
			Application.DoEvents();

			Application.Run(new Form1());
		}

		private void buttonFonts_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			FontFamily[] lFontFamilies;
			Font lFont;

			InstalledFontCollection lInstalledFontCollection = new InstalledFontCollection();

			buttonClearItems_Click(this, EventArgs.Empty);
			lFontFamilies = lInstalledFontCollection.Families;

			for(int j = 0; j < lFontFamilies.Length; ++j)
			{
				if (lFontFamilies[j].IsStyleAvailable(FontStyle.Regular))
				{
					lFont = new Font(lFontFamilies[j].Name, htmlDropDownList1.Font.Size);
					htmlDropDownList1.Items.Add("<font face=\"" + lFontFamilies[j].Name + "\">" + lFontFamilies[j].Name + "</font>"); 
					if (j % 5 == 0)
					{
						if (j == 0)
						{
							htmlDropDownList1.SelectedIndex = 0;
						}
						Application.DoEvents();
					}
				}
			}
			htmlDropDownList1.SelectedIndex = 0;		
			Cursor.Current = Cursors.Default;
		}

		private void buttonColors_Click(object sender, System.EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			buttonClearItems_Click(this, EventArgs.Empty);
			foreach (Color lColor in System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues())
			{
				htmlDropDownList1.Items.Add("<font color=\"" + lColor.ToKnownColor() + "\">" + lColor.ToKnownColor() + "</font><font color=\"black\"> " + (lColor.IsSystemColor == true ? "System Color" : string.Empty) + " HTML color=" + ColorTranslator.ToHtml(lColor) + " rgb=" + lColor.R.ToString("X2") + lColor.G.ToString("X2") + lColor.B.ToString("X2") + "</font>"); 
			}
			htmlDropDownList1.SelectedIndex = 0;		
			Cursor.Current = Cursors.Default;
		}
	
		protected override void OnLoad(EventArgs e)
		{
			comboBoxTextRendering.DataSource = Enum.GetValues(typeof(TextRenderingHint));

			base.OnLoad (e);
		}

		private void htmlDropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			textBoxHtml.Text = (string)htmlDropDownList1.SelectedItem;
		}

		private void buttonAdd_Click(object sender, System.EventArgs e)
		{
			int lSelectedIndex = htmlDropDownList1 .SelectedIndex;
			string lText = textBoxHtml.Text;

			try
			{
				if (htmlDropDownList1.DataSource != null)
				{	
					htmlDropDownList1.DataSource = null;
					htmlDropDownList1.Items.Clear();
				}

				htmlDropDownList1.Items.Add(lText);
				htmlDropDownList1.SelectedIndex = htmlDropDownList1.Items.Count - 1;
			}
			catch (Exception ex)
			{
				htmlDropDownList1.Items.RemoveAt(htmlDropDownList1.SelectedIndex);
				htmlDropDownList1.SelectedIndex = lSelectedIndex;
				textBoxHtml.Text = lText;
				MessageBox.Show(ex.Message);
			}
		}

		private void htmlDropDownList1_LinkClicked(object sender, FB.WinForms.Controls.HtmlDropDownList.LinkClickedEventArgs e)
		{
			FormLinkProperties lFormLinkProperties = new FormLinkProperties();

			lFormLinkProperties.HtmlDropDownList.Items.Clear();
			lFormLinkProperties.HtmlDropDownList.Items.Add((string)htmlDropDownList1.SelectedItem);
			lFormLinkProperties.HtmlDropDownList.SelectedIndex = 0;
			lFormLinkProperties.HtmlDropDownList.ImageList = imageListFlags;
			lFormLinkProperties.Form.Text = e.Id;
			lFormLinkProperties.InnerText.Text = e.InnerText;
			lFormLinkProperties.InnerHtml.Text = e.InnerHtml;

			if (lFormLinkProperties.ShowDialog() == DialogResult.OK)
			{
				if (lFormLinkProperties.InnerHtmlChanged == true)
				{
					htmlDropDownList1.ReplaceInnerHtml(e.Id, lFormLinkProperties.InnerHtml.Text);
				}
				else
				{
					if (lFormLinkProperties.InnerTextChanged == true)
					{
						htmlDropDownList1.ReplaceInnerText(e.Id, lFormLinkProperties.InnerText.Text);
					}
				}
			}
		}

		public void textBoxHtml_Update(string startTag)
		{
			if (textBoxHtml.SelectedText == string.Empty)
			{
				if (textBoxHtml.SelectionStart == 0)
				{
					textBoxHtml.Text += startTag;
				}
				else
				{
					textBoxHtml.Text = textBoxHtml.Text.Insert(textBoxHtml.SelectionStart, startTag);
				}
			}
		}

		private void buttonClearItems_Click(object sender, System.EventArgs e)
		{
			htmlDropDownList1.DataSource = null;
			htmlDropDownList1.Items.Clear();
		}

		public void textBoxHtml_Update(string startTag, string endTag)
		{
			if (textBoxHtml.SelectedText == string.Empty)
			{
				if (textBoxHtml.SelectionStart == 0)
				{
					textBoxHtml.Text += startTag + endTag;
				}
				else
				{
					textBoxHtml.Text = textBoxHtml.Text.Insert(textBoxHtml.SelectionStart, startTag + endTag);
				}
			}
			else
			{
				textBoxHtml.SelectedText = startTag + textBoxHtml.SelectedText + endTag;
			}
		}
	
		protected override void OnResize(EventArgs e)
		{
			float lFactor = (float)Height / 384F; // 384 is the initial height of the form
			htmlDropDownList1.Font = new Font(htmlDropDownList1.Font.Name, 20 * lFactor); // 20 is the initial font size of the HtmlDropDownList
			htmlDropDownList1.ItemHeight = (int)htmlDropDownList1.Font.GetHeight(CreateGraphics());
			base.OnResize (e);
		}

		private void buttonCountries_Click(object sender, System.EventArgs e)
		{
			buttonClearItems_Click(this, EventArgs.Empty);

			htmlDropDownList1.Items.Add("<img src=\"#0\"/> Australia"); 
			htmlDropDownList1.Items.Add("<img src=\"#1\"/> Canada"); 
			htmlDropDownList1.Items.Add("<img src=\"#2\"/> Chile"); 
			htmlDropDownList1.Items.Add("<img src=\"#3\"/> United Kingdom"); 
			htmlDropDownList1.Items.Add("<img src=\"#4\"/> United States"); 
			htmlDropDownList1.SelectedIndex = 0;		
		}

		private void buttonKeys_Click(object sender, System.EventArgs e)
		{
			buttonClearItems_Click(this, EventArgs.Empty);

			string[] lKeyNames = Enum.GetNames(typeof(Keys));
			Array lKeyValues = Enum.GetValues(typeof(Keys));
			ArrayList lKeyText = new ArrayList();
			for (int x=0; x < lKeyNames.Length; x++)
			{
				lKeyText.Add(x.ToString() + " Key name=" + lKeyNames[x] + " Key value=" + (int)lKeyValues.GetValue(x));
			}
			htmlDropDownList1.DataSource = lKeyText;
			htmlDropDownList1.SelectedIndex = 0;		
		}

		private void comboBoxTextRendering_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			htmlDropDownList1.TextRenderingHint = (TextRenderingHint)comboBoxTextRendering.Items[comboBoxTextRendering.SelectedIndex];
			htmlDropDownList1.Refresh();
		}

		private void buttonDefault_Click(object sender, System.EventArgs e)
		{
			buttonClearItems_Click(this, EventArgs.Empty);

			this.htmlDropDownList1.Items.AddRange(new object[] {   "<font color=\"Black\">Where the <B>file size</B> is greater than </font><a id=\"size\">size</a>.",
																   "<font color=\"Black\">Where the file was <I>changed</I> before </font><a id=\"date\">date</a>.",
																   "<font color=\"Black\">Where the file <U>contains</U> </font><a id=\"text\">text</a>.",
																   "<brush type=\"LinearGradient\" StartingColor=\"Red\" EndingColor=\"Blue\" GradientAngle=\"0\">Linear Gradient Brush Example</brush>",
																   "<brush type=\"Shadow\" offset=\"2\" shadowcolor=\"Blue\">Shadow Brush Example</brush>",
																   "<img src=\"#0\"/> Australia"});

			htmlDropDownList1.SelectedIndex = 0;		
		}

		private void htmlDropDownList1_SelectedIndexChanged_1(object sender, System.EventArgs e)
		{
			if (htmlDropDownList1.SelectedItem != null)
			{
				textBoxHtml.Text = htmlDropDownList1.SelectedItem.ToString();
			}
		}
	}
}
